package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.BaseContext;
import com.gaohe.common.R;
import com.gaohe.pojo.Classes;
import com.gaohe.pojo.Restore;
import com.gaohe.pojo.User;
import com.gaohe.pojo.UserClass;
import com.gaohe.service.ClassesService;
import com.gaohe.service.RestoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classes")
@Slf4j
public class ClassesController {
    @Autowired
    private ClassesService classesService;
    @Autowired
    private RestoreService restoreService;
    @GetMapping("/page")
    public R page(@RequestParam int page,
                  @RequestParam int pageSize,
                  @RequestParam(required = false) String name){
        Page<Classes> classesPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Classes::getName,name);
        classesService.page(classesPage,qw);
        return R.success(classesPage);
    }
    //分页查询回收站
    @GetMapping("/page1")
    public R page1(@RequestParam int page,
                   @RequestParam int pageSize,
                   @RequestParam(required = false) String name){
        Page<Restore> restorePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Restore> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Restore::getName,name);
        qw.orderByDesc(Restore::getUpdateTime);
        return R.success(restoreService.page(restorePage,qw));
    }
    //课程修改
    //修改回显
    @GetMapping("/{id}")
    public R findById(@PathVariable Long id){
       return R.success(classesService.getById(id)) ;
    }
    @PutMapping
    public R update(@RequestBody Classes classes){
        boolean b = classesService.updateById(classes);
        return b?R.success("修改成功"):R.error("修改失败");
    }
    //课程状态修改
    @DeleteMapping("/restore")
    public R restore(@RequestParam int id){
        Classes id1 = classesService.getById(id);
        boolean b = restoreService.removeById(id1);
        boolean b1 = classesService.save(id1);
        return b==b1?R.success("恢复成功"):R.error("恢复失败");
    }
    @DeleteMapping("/delete")
    public R delete(@RequestParam Long id){
        Classes byId = classesService.getById(id);
        boolean b = classesService.removeById(byId);
        int save = restoreService.save(byId);
        return b&&save>0?R.success("删除成功"):R.error("删除失败");

    }
    @PostMapping
    public R save1(@RequestBody Classes classes)
    {
        boolean save = classesService.save(classes);
        return save?R.success("新增成功"):R.success("新增失败");
    }
   //查询选择该课程的用户
   @GetMapping("/user/{id}")
   public R findByClasstoUsers(@PathVariable Long id) {
       List<User> byClasstoUsers = classesService.findByClasstoUsers(id);
       return R.success(byClasstoUsers);
   }
   @GetMapping("/getClassByCardTags")
    public R getClassByCardTags(@RequestParam String tags){
       List<String> classNames= classesService.getClassByCardTags(tags);
       return R.success(classNames);
   }
   //根据标签名称查询课程信息
    @GetMapping("/getClassList/{tag}")
    public R getClassListByTag(@PathVariable String tag){

        List<Classes> list=classesService.getClassByTag(tag);
       return R.success(list);
    }








}
